import time
from unittest import TestCase

from ddt import ddt, data, unpack
from selenium import webdriver

from FinanceData import ChangFinanceData, TermFinance, BayFund, BankPWD
from Web自动化测试.财务系统.代码.登陆测试.代码.LoginOperation import LoginOperation
from FinanceOperation import FinanceOperation


@ddt
class TestBuyFinance(TestCase):

    # 零钱理财买入
    @data(*ChangFinanceData.success_data)
    @unpack
    def testChangeFinance(self, username, password, paypassword):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()

        # 调用登陆
        log = LoginOperation()
        log.login(driver, username, password)

        # 支付前银行卡余额调用
        buy = FinanceOperation()
        money = buy.getBankCardMoney(driver, 0)
        print(len(money))
        time.sleep(3)
        print(money)
        # 调用零钱理财购买
        buy = FinanceOperation()
        result_buy = buy.buyFinance(driver, paypassword)
        time.sleep(3)
        result = buy.getBankCardMoney(driver, result_buy)
        print(result)
        if result_buy == "买入成功!":
            print(result)
            if int(money) != int(result) + 100:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money - 100, result)
        elif result_buy == "买入失败!":
            if int(money) == int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            self.assertEqual(money, result)
        elif result_buy == "买入失败!，银行卡余额不足":
            if int(money) != int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money, result)
        else:
            print(result)
            if money == result:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            self.assertEqual(money, result)

    # 期限理财买入
    @data(*TermFinance.success_data)
    @unpack
    def testTermFinance(self, username, password, paypassword):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()

        # 调用登陆
        log = LoginOperation()
        log.login(driver, username, password)

        # 支付前银行卡余额调用
        buy = FinanceOperation()
        money = buy.getBankCardMoney(driver, 0)
        print(len(money))
        time.sleep(3)
        print(money)
        # 调用期限理财购买
        buy = FinanceOperation()
        result_buy = buy.buyTermFinance(driver, paypassword)
        time.sleep(3)
        result = buy.getBankCardMoney(driver, result_buy)
        print(result)
        if result_buy == "买入成功!":
            print(result)
            if int(money) != int(result) + 5000:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money - 5000, result)
        elif result_buy == "买入失败!":
            if int(money) != int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money, result)
        elif result_buy == "买入失败!，银行卡余额不足":
            if int(money) != int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money, result)
        else:
            print(result)
            if money == result:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            self.assertEqual(money, result)

    # # 基金理财买入
    @data(*BayFund.success_data)
    @unpack
    def testBuyFund(self, username, password, paypassword):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()

        # 调用登陆
        log = LoginOperation()
        log.login(driver, username, password)

        # 支付前银行卡余额调用
        buy = FinanceOperation()
        money = buy.getBankCardMoney(driver, 0)
        print(len(money))
        time.sleep(3)
        print(money)
        # 调用基金理财购买
        buy = FinanceOperation()
        result_buy = buy.buyBuyFund(driver, paypassword)
        time.sleep(3)
        result = buy.getBankCardMoney(driver, result_buy)
        print(result)
        if result_buy == "买入成功!":
            print(result)
            if int(money) != int(result) + 3000:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money - 3000, result)
        elif result_buy == "买入失败!":
            if int(money) != int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money, result)
        elif result_buy == "买入失败!，银行卡余额不足":
            if int(money) != int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money, result)
        else:
            print(result)
            if money == result:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            self.assertEqual(money, result)

    # 银行卡支付
    @data(*BankPWD.success_data)
    @unpack
    def testBankPWD(self, username, password, paypassword):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()

        # 调用登陆
        log = LoginOperation()
        log.login(driver, username, password)

        # 支付前银行卡余额调用
        buy = FinanceOperation()
        money = buy.getBankCardMoney(driver, '0')
        print(len(money))
        time.sleep(3)
        print(money)
        # 调用基金理财购买
        buy = FinanceOperation()
        result_buy = buy.buyBuyFund(driver, paypassword)
        time.sleep(3)
        result = buy.getBankCardMoney(driver, result_buy)
        print(result)
        if result_buy == "买入成功!":
            print(result)
            if int(money) != int(result) + 3000:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money - 3000, result)
        elif result_buy == "买入失败!，请绑定银行卡":
            if money != result:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            self.assertEqual(money, result)
        elif result_buy == "买入失败!,请前往个人信息设置支付密码":
            if int(money) != int(result):
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            money = int(money)
            result = int(result)
            self.assertEqual(money, result)
        else:
            print(result)
            if money != result:
                driver.save_screenshot(username + "---" + password + ".jpg")
            else:
                print("测试成功！")
            driver.quit()
            self.assertEqual(money, result)

    # 更改默认卡
    @data(*BankPWD.data2)
    @unpack
    def testBankPWD2(self, username, password, paypassword):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()

        # 调用登陆
        log = LoginOperation()
        log.login(driver, username, password)
        # 更换默认卡
        buy = FinanceOperation()
        chang = buy.changBank(driver)
        # 支付前银行卡余额调用
        money = buy.getBankCardMoney2(driver)
        print(len(money))
        time.sleep(3)
        print(money)

        # 调用基金理财购买
        buy = FinanceOperation()
        result_buy = buy.buyBuyFund(driver, paypassword)
        time.sleep(3)
        result = buy.getBankCardMoney2(driver)
        print(result)
        if int(money) != int(result) + 3000:
            driver.save_screenshot(username + "---" + password + ".jpg")
        else:
            print("测试成功！")
        driver.quit()
        money = int(money)
        result = int(result)
        self.assertEqual(money - 3000, result)
